package demo2;  //接口使用案例
import java.util.Arrays;
import java.util.Comparator;

/**
 * @Author 12629
 * @Description：
 */
/*class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //return this.age - o.age;
        *//*if(this.age > o.age) {
            return 1;
        }else if(this.age < o.age) {
            return -1;
        }else {
            return 0;
        }*//*
        return this.age - o.age;
    }
}*/

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int compareTo(Student o) {
        return this.age - o.age;
    }
}

//优点  对类的侵入性 不强
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo( o2.name) ;
    }
}

public class Test {

    public static void main(String[] args) {
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("lisi",15);

        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student1, student2));

        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1,student2));
    }


    //冒泡排序
    public static void bubbleSort(Comparable[] comparable) {
        for (int i = 0; i < comparable.length-1; i++) {
            for (int j = 0; j < comparable.length-1-i; j++) {
                if(comparable[j].compareTo(comparable[j+1]) > 0) {
                    Comparable tmp = comparable[j];
                    comparable[j] = comparable[j+1];
                    comparable[j+1] = tmp;
                }
            }
        }
    }



    public static void main1(String[] args) {
        /*Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("lisi",15);

        System.out.println(student1.compareTo(student2));//student1这个对象和student2这个对象比较
*/
        /*int a = 10;
        int b =20;
        System.out.println(a > b);*/

        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10);
        students[1] = new Student("lisi",2);
        students[2] = new Student("wangmazi",18);

        //Arrays.sort(students);
        //bubbleSort(students);
        System.out.println(Arrays.toString(students));


    }

}
